package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.ILoginLogDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.LoginLog;
import cn.edu.seu.ssny.entity.User;
import cn.edu.seu.ssny.service.ILoginLogService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class LoginLogServiceImpl extends BaseServiceImpl<LoginLog, Integer> implements ILoginLogService {

    @Autowired
    private ILoginLogDao loginLogDao;

    @Override
    public IBaseDao<LoginLog, Integer> getBaseDao() {
        return this.loginLogDao;
    }

    @Override
    public List<LoginLog> findByUser(User user) {
        return loginLogDao.findByUser(user);
    }

    @Override
    public void login(User user, Enterprise enterprise){
        LoginLog log = new LoginLog();
        log.setUser(user);
        log.setEnterprise(enterprise);
        log.setLoginTime(new Date());
        save(log);
    }

    @Override
    public void logout(User user) {
        LoginLog log = loginLogDao.findFirstByUserOrderByLoginTimeDesc(user);
        log.setLogoutTime(new Date());
        update(log);
    }
}